import Cookies from 'js-cookie'

export function getGenericDomain(){
  let host = window.location.hostname
  let regIp=/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
  if(regIp.test(host)||'localhost'===host){
    return  host
  }
  let regDomainSuffix=/.(com.cn|net.cn|gov.cn|org.nz|org.cn|com|net|org|gov|cc|biz|info|cn|co)$/
  let temp=host.replace(regDomainSuffix, "")
  let execArray=regDomainSuffix.exec(host)
  if(execArray instanceof Array){
    let suffix=execArray[0];
    let realDomain=temp.substring(temp.lastIndexOf("."));
    return temp.indexOf(".")!==-1?realDomain+suffix:'.'+realDomain+suffix
  }
  return execArray
}


// 获取cookie
export function getCookie(name) {
  return Cookies.get(name, {domain: getGenericDomain() })
}

// 设置cookie
export function setCookie(name, value, day) {
  Cookies.set(name, value, { expires: day, path: '/',domain: getGenericDomain() })
}

// 删除cookie
export function delCookie(name) {
  Cookies.remove(name,{domain: getGenericDomain() })
}
